package com.zengqingfa.juc.n3;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author zengqingfa
 * @className TestSleepAndYield
 * @description
 * @create 2021/6/6 13:40
 */
@Slf4j(topic = "c.TestSleepAndYield")
public class TestSleepAndYield {

    static Object monitor = new Object();

    public static void main(String[] args) throws InterruptedException {
        /**
         * 21:18:36:493 [t1] c.TestSleepAndYield - 进入同步器锁
         * 21:18:37:493 [main] c.TestSleepAndYield - main 线程
         * 21:18:39:494 [main] c.TestSleepAndYield - main线程进入同步器锁
         */
        Thread t1 = new Thread("t1") {
            @Override
            public void run() {
                synchronized (monitor) {
                    log.debug("进入同步器锁");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t1.start();
        Thread.sleep(1000);
        log.debug("main 线程");
        synchronized (monitor) {
            log.debug("main线程进入同步器锁");
        }
    }

    private static void mehod3() {
        Runnable task1 = () -> {
            int count = 0;
            for (; ; ) {
                System.out.println("---->1 " + count++);
            }
        };
        Runnable task2 = () -> {
            int count = 0;
            for (; ; ) {
//                 Thread.yield();
                System.out.println(" ---->2 " + count++);
            }
        };
        Thread t1 = new Thread(task1, "t1");
        Thread t2 = new Thread(task2, "t2");
        t1.setPriority(Thread.MIN_PRIORITY);
        t2.setPriority(Thread.MAX_PRIORITY);
        t1.start();
        t2.start();
    }

    private static void method1() throws InterruptedException {
        Thread t1 = new Thread("t1") {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    /**
                     * 13:49:26 [t1] c.TestSleepAndYield - 线程被打断：sleep interrupted
                     * java.lang.InterruptedException: sleep interrupted
                     * 	at java.lang.Thread.sleep(Native Method)
                     * 	at com.zengqingfa.juc.n3.TestSleepAndYield$1.run(TestSleepAndYield.java:19)
                     */
                    log.debug("线程被打断：{}", e.getMessage());
                    e.printStackTrace();
                }
            }
        };
        t1.start();
        Thread.sleep(100);
        t1.interrupt();

        TimeUnit.SECONDS.sleep(100);
    }

    private static void method2() throws InterruptedException {
        /**
         * 13:44:57 [main] c.TestSleepAndYield - t1 state:NEW
         * 13:44:57 [main] c.TestSleepAndYield - t1 state:RUNNABLE
         * 13:44:57 [main] c.TestSleepAndYield - t1 state:TIMED_WAITING
         * 13:44:59 [t1] c.TestSleepAndYield - t1...run
         */
        Thread t1 = new Thread("t1") {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("t1...run");
            }
        };
        log.debug("t1 state:{}", t1.getState());
        t1.start();
        log.debug("t1 state:{}", t1.getState());
        Thread.sleep(100);
        log.debug("t1 state:{}", t1.getState());
    }
}